<template>
  <div class="category">
    <el-button
      type="primary"
      class="mb-10"
      @click="
        dialogVisible = true;
        isAdd = true;
        formData = JSON.parse(JSON.stringify(initFormData));
      "
      >添加分类</el-button
    >
    <el-table :data="tableData" stripe>
      <el-table-column label="序号" width="60">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="_id" label="id" />
      <el-table-column label="分类图标">
        <template slot-scope="scope">
          <img :src="$getImageUrl(scope.row.cover)" width="45" alt="" />
        </template>
      </el-table-column>
      <el-table-column prop="name" label="分类名称" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <div class="operation-wrap">
            <el-button type="primary" @click="editRow(scope.row)"
              >编辑</el-button
            >
            <el-popconfirm
              confirmButtonText="确定"
              cancelButtonText=""
              icon="el-icon-info"
              iconColor="red"
              :title="`确定删除${scope.row.name}吗？`"
              @onConfirm="deleteRow(scope.row)"
            >
              <el-button type="danger" slot="reference">删除</el-button>
            </el-popconfirm>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :title="isAdd ? '添加' : '编辑'"
      :visible.sync="dialogVisible"
      width="45%"
    >
      <el-form ref="form" :model="formData" label-width="120px">
        <el-form-item label="分类名称">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="分类图标">
          <el-upload
            class="avatar-uploader"
            :action="uploadUrl"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :headers="uploadHeaders"
          >
            <img v-if="formData.cover" :src="formData.cover" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitCommonRow()">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getCat, addCat, editCat, deleteCat } from "@/api/category";
import { Message, MessageBox } from "element-ui";
import { getToken } from "@/utils/auth";
export default {
  name: "",
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      formData: {
        name: "",
        cover: ""
      },
      initFormData: {
        name: "",
        cover: ""
      },
      isAdd: false,
      editRowData: {},
      uploadHeaders: { Authorization: `Bearer ${getToken()}` },
      uploadUrl: process.env.VUE_APP_UPLOAD
    };
  },
  created() {
    this.getCat_com();
  },
  methods: {
    getCat_com() {
      getCat().then(res => {
        if (res.code === 200) {
          this.tableData = res.data;
        }
      });
    },
    handleAvatarSuccess(res, file) {
      console.log(res, file);
      this.formData.cover = `${process.env.VUE_APP_FILE}/${res.originalname}`;
    },
    editRow(row) {
      this.isAdd = false;
      this.dialogVisible = true;
      this.editRowData = row;
      const { name, cover } = row;
      this.formData = { name, cover: this.$getImageUrl(cover) };
    },
    submitCommonRow() {
      console.log(this.formData);
      console.log(this.editRowData);
      if (this.isAdd) {
        addCat(this.formData).then(res => {
          this.dialogVisible = false;
          if (res.code === 200) {
            this.getCat_com();
            Message({ type: "success", message: "新增成功" });
          }
        });
      } else {
        editCat({ ...this.formData, id: this.editRowData._id }).then(res => {
          this.dialogVisible = false;
          if (res.code === 200) {
            this.getCat_com();
            Message({ type: "success", message: "修改成功" });
          }
        });
      }
    },
    deleteRow(row) {
      deleteCat({ id: row._id }).then(res => {
        if (res.code === 200) {
          this.getCat_com()
          Message({ type: "success", message: "删除成功" });
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.category {
  padding: 30px;
  ::v-deep .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 120px;
    height: 120px;
    line-height: 120px;
    text-align: center;
  }
  .avatar {
    width: 120px;
    height: 120px;
    display: block;
  }
}
</style>
